package com.jiangyg.mall.coupon.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyg.mall.core.enums.Enable;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import com.jiangyg.mall.coupon.constant.ColumnConstant;
import com.jiangyg.mall.coupon.dao.SeckillSessionDao;
import com.jiangyg.mall.coupon.dto.SeckillSessionDTO;
import com.jiangyg.mall.coupon.entity.SeckillSession;
import com.jiangyg.mall.coupon.vo.SeckillSessionVO;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SeckillSessionServiceImpl extends BaseServiceImpl<SeckillSessionDao, SeckillSession> implements SeckillSessionService {

    @Autowired
    public SeckillSessionServiceImpl(SeckillSessionDao seckillSessionDao) {
        super(seckillSessionDao);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<SeckillSessionVO> selectListByPromotionId(Long promotionId) throws Exception {
        final QueryWrapper<SeckillSession> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.PROMOTION_ID, promotionId);
        wrapper.eq(ColumnConstant.STATUS, Enable.Y.icode);
        final List<SeckillSession> dataList = this.baseMapper.selectList(wrapper);
        return BeanUtils.transformList(dataList, SeckillSessionVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(SeckillSessionDTO dto) throws Exception {
        return this.executeSaveOrUpdate(dto) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public SeckillSessionVO selectById(Long seckillSessionId) throws Exception {
        return this.executeSelectById(seckillSessionId, SeckillSessionVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> seckillSessionIds) {
        return this.baseMapper.deleteBatchIds(seckillSessionIds) > 0;
    }

}
